package com.mainactivity.usbhost;

import android.text.TextUtils;
import android.util.Log;

public class ByteUtil {
	
	public static String byte2String(byte[] bytes){
		return byte2String(bytes, " ");
	}
	
	public static String byte2String(byte[] bytes, String split){
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < bytes.length; i++) {
			if (i != 0 && !TextUtils.isEmpty(split)) {
				sb.append(split);
			}
			sb.append(getHex(bytes[i]));
		}
		return sb.toString();
	}
	
	/**
	 * 把16进制数字近些format
	 * 
	 * @param data
	 * @return
	 */
	public static String getHex(byte data) {
		String hex = Integer.toHexString((data & 0xFF));
		if (hex.length() == 1)
			hex = '0' + hex;
		else if (hex.length() > 2)
			hex = hex.substring(hex.length() - 2, hex.length());
		return hex;
	}
	
	public static String getHexForByte(byte data){
		String hex = Integer.toHexString((data & 0xFF));
		if (hex.length() == 1)
			hex = '0' + hex;
		else if (hex.length() >2)
			hex = hex.substring(hex.length() - 2 , hex.length());
		return hex;
		
	}
	/**
	 * 替换校验码
	 * @param data
	 * @return
	 */
	public static byte crc(byte[] data){
		byte pwd = 0x00;
		for (int i = 1 ; i < data.length - 2 ; i ++){
			pwd = (byte)(pwd ^ data[i]);
		}
		pwd = (byte) ~ pwd;
		return pwd;
	}
	
	/**
	 * 替换用户数据
	 * @param oldByte
	 * @param newByte
	 * @param length
	 */
	public static void replaceByte(byte[] oldByte , byte[] newByte , int length){
		for (int i = 0 ; i < length ; i++){
			oldByte[8 + i] = newByte[i];
		}
	}
	
	public static byte[] addByte(byte heightByte, byte lowByte, byte step){
		byte[] resultB = new byte[2];
		
		long x = Long.parseLong("f1", 16);
		long y = Long.parseLong("f2", 16);
		System.out.println(Long.toHexString(x + y));
		
		return resultB;
	}
	
	/**
	 * 
	 * @param step 200(0xc8)
	 * @param time 第几次偏移
	 * @return byte[]
	 */
	public static byte[] getOffsetIndex(int step, int time){
		byte[] resultB = new byte[2];
		int x = step * time;
		byte[] ss = int2Bytes(x);
		resultB[0] = ss[2];
		resultB[1] = ss[3];
		return resultB;
	}
	
	public static byte[] int2Bytes(int num) {  
        byte[] byteNum = new byte[4];  
        for (int ix = 0; ix < 4; ++ix) {  
            int offset = 32 - (ix + 1) * 8;  
            byteNum[ix] = (byte) ((num >> offset) & 0xff);  
        }  
        return byteNum;  
    }
	
	
	public static byte[] replaceFilePath(String inputStr , byte[] array , boolean isWrite){
		String hexStr = inputStr.substring(2);
		int fileLocation = Integer.parseInt(hexStr, 16);
		
		if (isWrite == true){
			Log.i("MainActivity", "fillocation-->" + fileLocation  + ",appen-->" + (fileLocation + 0x80));
			array[6] = (byte)(fileLocation + 0x80);
			Log.i("MainActivity", "write-->" + array[6]);
		}else{ 
			array[5] = (byte)fileLocation;
			Log.i("MainActivity", "read-->" + array[5]);
		}
		return array;
	}

	/***
	 * 移位字节数据
	 * @param dataA
	 * @param dataB
     * @return
     */
	public static byte[] append(byte[] dataA, byte[] dataB) {
		byte[] resultB = new byte[dataA.length + dataB.length];
		System.arraycopy(dataA, 0, resultB, 0, dataA.length);
		System.arraycopy(dataB, 0, resultB, dataA.length, dataB.length);
		return resultB;
	}
	
}
